package com.attendancesystem.service;


import com.attendancesystem.model.Lecture;
import com.attendancesystem.util.JDBCUtils;
import jakarta.servlet.http.HttpServletResponse;

import java.sql.*;
import java.util.List;

public class LectureService {
    public Lecture getLectureDetail(String courseId) throws SQLException {
        // 示例数据库查询逻辑
        String sql ="select l.* from lectures l join courses c on l.course_id = c.course_id where c.course_id=?";

        try ( Connection con = JDBCUtils.getConnection(); // 必须从连接池获取
              PreparedStatement stmt = con.prepareStatement(sql);) {

            stmt.setString(1, courseId);
            try (ResultSet rs = stmt.executeQuery()) {
               if (rs.next()) {
                    Lecture detail=new Lecture();
                    detail.setLectureId(rs.getInt("lecture_id"));
                    detail.setCourseId(rs.getInt("course_id"));
                    detail.setClassName(rs.getString("class_name"));
                    detail.setLectureTime(rs.getTimestamp("lecture_time").toLocalDateTime());
                    detail.setClassroom(rs.getString("classroom"));
                    detail.setSummary(rs.getString("summary"));
                    return detail;
                }
            }
        }
        return null;
    }
    public void updateLecture(Lecture lecture) throws SQLException {
        String sql = "UPDATE lectures SET " +
                "class_name = ?, " +
                "lecture_time = ?, " +
                "classroom = ?, " +
                "summary = ? " +
                "WHERE course_id = ?";

        try ( Connection con = JDBCUtils.getConnection(); // 必须从连接池获取
              PreparedStatement pstmt = con.prepareStatement(sql)) {
            pstmt.setString(1, lecture.getClassName());
            pstmt.setTimestamp(2, Timestamp.valueOf(lecture.getLectureTime()));
            pstmt.setString(3, lecture.getClassroom());
            pstmt.setString(4, lecture.getSummary());
            pstmt.setInt(5, lecture.getCourseId());
                int affectedRows = pstmt.executeUpdate();

                // 处理更新结果
                if (affectedRows == 0) {
                    throw new SQLException("更新失败，未找到对应课程");
                }


        }
    }
    public void deleteLecture(String LectureId) throws SQLException {
        String sql = "DELETE FROM lectures WHERE lecture_id = ?";
        try(Connection con = JDBCUtils.getConnection(); // 必须从连接池获取
            PreparedStatement pstmt = con.prepareStatement(sql)){
            pstmt.setInt(1, Integer.parseInt(LectureId));
            int affectedRows = pstmt.executeUpdate();
            if (affectedRows == 0) {
                throw new SQLException("删除失败，未找到对应课程");
            }

        }catch (SQLException e)
        {
            throw new SQLException("数据库访问失败", e);
        }

    }
    //批量删除
    public boolean BatchDeleteLectures(int LectureIds) throws SQLException {
        String sql = "DELETE FROM lectures WHERE lecture_id = ?";
        try (Connection con = JDBCUtils.getConnection(); // 必须从连接池获取
             PreparedStatement pstmt = con.prepareStatement(sql)) {
            pstmt.setInt(1, LectureIds);
            return pstmt.executeUpdate() > 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

}
